package com.lrkj.notice.service.impl;

import com.lrkj.common.core.domain.entity.SysDictData;
import com.lrkj.common.enums.NoticeType;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.notice.domain.NoticeSiteMessageUser;
import com.lrkj.notice.mapper.NoticeSiteMessageUserMapper;
import com.lrkj.notice.service.INoticeSiteMessageUserService;
import com.lrkj.notice.vo.MyNoticeVo;
import com.lrkj.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息关联用户Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-09-07
 */
@Service
public class NoticeSiteMessageUserServiceImpl implements INoticeSiteMessageUserService {
    @Autowired
    private NoticeSiteMessageUserMapper noticeSiteMessageUserMapper;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询消息关联用户
     *
     * @param id 消息关联用户ID
     * @return 消息关联用户
     */
    @Override
    public NoticeSiteMessageUser selectNoticeSiteMessageUserById(String id) {
        return noticeSiteMessageUserMapper.selectNoticeSiteMessageUserById(id);
    }

    /**
     * 查询消息关联用户列表
     *
     * @param noticeSiteMessageUser 消息关联用户
     * @return 消息关联用户
     */
    @Override
    public List<NoticeSiteMessageUser> selectNoticeSiteMessageUserList(NoticeSiteMessageUser noticeSiteMessageUser) {
        return noticeSiteMessageUserMapper.selectNoticeSiteMessageUserList(noticeSiteMessageUser);
    }

    /**
     * 新增消息关联用户
     *
     * @param noticeSiteMessageUser 消息关联用户
     * @return 结果
     */
    @Override
    public int insertNoticeSiteMessageUser(NoticeSiteMessageUser noticeSiteMessageUser) {
        noticeSiteMessageUser.setCreateTime(DateUtils.getNowDate());
        return noticeSiteMessageUserMapper.insertNoticeSiteMessageUser(noticeSiteMessageUser);
    }

    /**
     * 修改消息关联用户
     *
     * @param noticeSiteMessageUser 消息关联用户
     * @return 结果
     */
    @Override
    public int updateNoticeSiteMessageUser(NoticeSiteMessageUser noticeSiteMessageUser) {
        return noticeSiteMessageUserMapper.updateNoticeSiteMessageUser(noticeSiteMessageUser);
    }

    /**
     * 批量删除消息关联用户
     *
     * @param ids 需要删除的消息关联用户ID
     * @return 结果
     */
    @Override
    public int deleteNoticeSiteMessageUserByIds(String[] ids) {
        return noticeSiteMessageUserMapper.deleteNoticeSiteMessageUserByIds(ids);
    }

    /**
     * 删除消息关联用户信息
     *
     * @param id 消息关联用户ID
     * @return 结果
     */
    @Override
    public int deleteNoticeSiteMessageUserById(String id) {
        return noticeSiteMessageUserMapper.deleteNoticeSiteMessageUserById(id);
    }

    @Override
    public int deleteByMessageId(String messageId) {
        return noticeSiteMessageUserMapper.deleteByMessageId(messageId);
    }

    @Override
    public int updateHaveRead(String id) {
        return noticeSiteMessageUserMapper.updateHaveRead(id);
    }

    @Override
    public int notReadMsgCountRead(String receiveId) {
        return noticeSiteMessageUserMapper.notReadMsgCountRead(receiveId);
    }

    @Override
    public int updateNoRead(String receiveId) {
        return noticeSiteMessageUserMapper.updateNoRead(receiveId);
    }

    @Override
    public List<MyNoticeVo> noticeGroupList(String userName) {
        List<MyNoticeVo> results = this.noticeGroup();
        Map<String, MyNoticeVo> groupListMap = this.getGroupListMap(userName);
        Map<String, MyNoticeVo> groupNumMap = this.getGroupNumMap(userName);

        results.stream().forEach(x -> {
            if (groupListMap.containsKey(x.getMessageType())) {
                MyNoticeVo noticeVo = groupListMap.get(x.getMessageType());
                x.setMessageUserId(noticeVo.getMessageUserId());
                x.setLastContent(noticeVo.getLastContent());
                x.setLastSenderTime(noticeVo.getLastSenderTime());
                x.setLastSenderName(noticeVo.getLastSenderName());
            }
            if (groupNumMap.containsKey(x.getMessageType())) {
                x.setReadNum(groupNumMap.get(x.getMessageType()).getReadNum());
            }
        });

        return results;
    }

    //获取消息分组数量
    private Map<String, MyNoticeVo> getGroupNumMap(String userName) {
        Map<String, MyNoticeVo> noticeVoMap = new HashMap<>();
        MyNoticeVo eg = new MyNoticeVo();
        eg.setReceiveId(userName);
        List<MyNoticeVo> myNoticeVos = noticeSiteMessageUserMapper.noticeGroupNum(eg);
        myNoticeVos.stream().forEach(x -> {
            if (!noticeVoMap.containsKey(x.getMessageType())) {
                noticeVoMap.put(x.getMessageType(), x);
            }
        });
        return noticeVoMap;
    }

    //获取消息分组列表
    private Map<String, MyNoticeVo> getGroupListMap(String userName) {
        Map<String, MyNoticeVo> noticeVoMap = new HashMap<>();
        List<MyNoticeVo> myNoticeVos = noticeSiteMessageUserMapper.noticeGroupList(userName);
        myNoticeVos.stream().forEach(x -> {
            if (!noticeVoMap.containsKey(x.getMessageType())) {
                noticeVoMap.put(x.getMessageType(), x);
            }
        });
        return noticeVoMap;
    }

    //先把字典内容转换成分组
    public List<MyNoticeVo> noticeGroup() {
        List<MyNoticeVo> myNoticeVos = new ArrayList<>();
        List<SysDictData> sysDictData = dictTypeService.selectDictDataByType(NoticeType.MESSAGE_TYPE.getCode());
        sysDictData.stream().forEach(x -> {
            MyNoticeVo noticeVo = new MyNoticeVo();
            noticeVo.setMessageType(x.getDictValue());
            noticeVo.setMessageName(x.getDictLabel());
            myNoticeVos.add(noticeVo);
        });
        return myNoticeVos;
    }

    @Override
    public List<MyNoticeVo> noticeGroupNum(MyNoticeVo myNoticeVo) {
        return noticeSiteMessageUserMapper.noticeGroupNum(myNoticeVo);
    }


    /**
     * 批量新增消息关联用户信息
     *
     * @param noticeSiteMessageUsers 消息关联用户对象
     */
    public int batchAddData(List<NoticeSiteMessageUser> noticeSiteMessageUsers) {
        return noticeSiteMessageUserMapper.batchAddData(noticeSiteMessageUsers);
    }
}
